package com.zhiyou100.crm.servlet.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.User;
import com.zhiyou100.crm.service.UserService;
import com.zhiyou100.crm.service.impl.UserServiceImpl;

/**
 * 更新用户
 */
@WebServlet("/user/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	UserService userService=new UserServiceImpl();
	List<User> users=new ArrayList<>();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String id = request.getParameter("id");
		users=userService.queryAllUser();
		if(id!=null) {
			int id1=Integer.parseInt(id);
			for (int i = 0; i < users.size(); i++) {
				int user_id = users.get(i).getUser_id();
				if (id1==user_id) {
					request.setAttribute("user", users.get(i));
					System.out.println("我是" + users.get(i).toString());
					break;
				}
			}
		}

		request.getRequestDispatcher("/WEB-INF/view/user/update.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String password = request.getParameter("password");
		String mobile = request.getParameter("mobile");
		String tel = request.getParameter("tel");
		int userId = Integer.parseInt(request.getParameter("userId"));
		Boolean isMale = Boolean.valueOf(request.getParameter("isMale"));
		User user=new User(userId,password,mobile,isMale,tel);
		Boolean b=userService.updateUser(user);
		if(b){
			response.sendRedirect(request.getContextPath()+"/user/list");
		}else{
			request.getRequestDispatcher("/WEB-INF/view/user/update.jsp").forward(request, response);
		}
//		String isMale = request.getParameter("isMale");
//		String mobile = request.getParameter("mobile");
//		String address = request.getParameter("address");
//		String age = request.getParameter("age");
//		String tel = request.getParameter("tel");
//		String idNum = request.getParameter("idNum");
//		String email = request.getParameter("email");
//		String qq = request.getParameter("qq");
//		String hobby = request.getParameter("hobby");
//		String education = request.getParameter("education");
//		String cardNum = request.getParameter("cardNum");
//		String nation = request.getParameter("nation");
//		String marry = request.getParameter("marry");
//		String remark = request.getParameter("remark");

	}

}
